package id.co.rho.scoring.cid.server.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import id.co.rho.scoring.cid.domain.Criteria;
import id.co.rho.scoring.cid.domain.Model;
import id.co.rho.scoring.cid.server.dao.CriteriaDao;
import id.co.rho.scoring.cid.server.dao.GenericDao;
import id.co.rho.scoring.cid.server.service.ScoringModelService;

@Service
@Transactional
public class ScoringModelServiceImpl implements ScoringModelService {

	@PersistenceContext
	EntityManager entityManager;
	private CriteriaDao criteriaDao;
	private GenericDao genericDao;

	@Autowired
	public void setGenericDao(GenericDao genericDao) {
		this.genericDao = genericDao;
	}

	@Autowired
	public void setCriteriaDao(CriteriaDao criteriaDao) {
		this.criteriaDao = criteriaDao;
	}

	@Override
	public List<Criteria> getAllCriteria() {
		return criteriaDao.getAllCriteria();
	}
	
	@Override
	public List<Criteria> getCriteriaByModelId(Long modelId) {
		Model model = genericDao.find(Model.class, modelId);
		return model.getCriteries();
	}
	
	@Override
	public Criteria getCriteriaById(Long id){
		return genericDao.find(Criteria.class, id);
	}
	
	@Override
	public Criteria saveOrUpdate(Criteria criteria) {
		Criteria result = null;
		result = genericDao.saveOrUpdate(criteria);
		return result;
	}

	@Override
	public List<Criteria> getCriteriaByParentId(Long parentId) {
		return criteriaDao.getCriteriaByParent(parentId);
	}

	@Override
	public Model getModelById(Long id) {
		return genericDao.find(Model.class, id);
	}

	@Override
	public Model saveOrUpdateModel(Model model){
		Model result = null;
		result = genericDao.saveOrUpdate(model);
		return result;
	}

	@Override
	public String setAsDefault(Long id, String modelName) {
		return criteriaDao.setAsDefault(id, modelName);
	}
	
	@Override
	public Integer getModelRev(String modelName) {
		return criteriaDao.getModelRev(modelName);
	}
	
	@Override
	public void deleteModel(Model model){
		genericDao.remove(model);
	}

}
